const router = require("express").Router();
const AdminDailyReport = require("../mysql/collection/AdminDailyReport");
const uuid = require("uuid");

/**
 * @api {post} /admin/dailyReport/create 创建日报
 * @apiName CreateDailyReport
 * @apiGroup DailyReport
 * @apiHeader {String} token token
 * @apiParam {String} title 日报标题
 * @apiParam {String} content 日报内容
 * @apiParam {String} creatorId 创建者ID
 * @apiParam {String} creatorName 创建者名称
 */
router.post("/create", (req, res) => {
  const { title, content, creatorId, creatorName } = req.body;
  if (!title || !content || !creatorId) {
    return res.status(400).json({ code: 400, message: "标题、内容和创建者ID不能为空" });
  }

  const report = new AdminDailyReport({
    reportId: "report_" + uuid.v4(),
    title,
    content,
    creatorId,
    creatorName,
    status: 0,
    createTime: new Date(),
    updateTime: new Date(),
    flag: true
  });

  report.save().then(() => {
    res.json({ code: 200, message: "创建日报成功", data: report });
  }).catch(err => {
    res.status(500).json({ code: 500, message: err.message });
  });
});

/**
 * @api {get} /admin/dailyReport/list 获取日报列表
 * @apiName GetDailyReportList
 * @apiGroup DailyReport
 * @apiHeader {String} token token
 * @apiParam {Number} [pageSize=10] 每页数量
 * @apiParam {Number} [pageNum=1] 当前页码
 */
router.get("/list", (req, res) => {
  const { pageSize = 10, pageNum = 1 } = req.query;
  const skip = (pageNum - 1) * pageSize;

  Promise.all([
    AdminDailyReport.find({ flag: true })
      .skip(parseInt(skip))
      .limit(parseInt(pageSize))
      .sort({ createTime: -1 }),
    AdminDailyReport.countDocuments({ flag: true })
  ]).then(([list, total]) => {
    res.json({
      code: 200,
      data: {
        list,
        total,
        pageSize: parseInt(pageSize),
        pageNum: parseInt(pageNum)
      }
    });
  }).catch(err => {
    res.status(500).json({ code: 500, message: err.message });
  });
});

/**
 * @api {put} /admin/dailyReport/update/:id 更新日报
 * @apiName UpdateDailyReport
 * @apiGroup DailyReport
 * @apiHeader {String} token token
 * @apiParam {String} id 日报ID
 * @apiParam {String} [title] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Number} [status] 状态
 */
router.put("/update/:id", (req, res) => {
  const { id } = req.params;
  const { title, content, status } = req.body;

  AdminDailyReport.findOneAndUpdate(
    { reportId: id },
    { 
      $set: { 
        ...(title && { title }),
        ...(content && { content }),
        ...(status && { status }),
        updateTime: new Date() 
      } 
    },
    { new: true }
  ).then(report => {
    if (!report) {
      return res.status(404).json({ code: 404, message: "日报不存在" });
    }
    res.json({ code: 200, message: "更新日报成功", data: report });
  }).catch(err => {
    res.status(500).json({ code: 500, message: err.message });
  });
});

/**
 * @api {delete} /admin/dailyReport/delete/:id 删除日报
 * @apiName DeleteDailyReport
 * @apiGroup DailyReport
 * @apiHeader {String} token token
 * @apiParam {String} id 日报ID
 */
router.delete("/delete/:id", (req, res) => {
  const { id } = req.params;

  AdminDailyReport.findOneAndUpdate(
    { reportId: id },
    { $set: { flag: false, updateTime: new Date() } },
    { new: true }
  ).then(report => {
    if (!report) {
      return res.status(404).json({ code: 404, message: "日报不存在" });
    }
    res.json({ code: 200, message: "删除日报成功", data: report });
  }).catch(err => {
    res.status(500).json({ code: 500, message: err.message });
  });
});

module.exports = router;